<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class Search extends MX_Controller
{

    private $_langCode;

    function __construct()
    {
        parent::__construct();
        $this->load->library('Layout');
        $this->_langCode = $this->mycommon->getLangCode();
        $this->load->model('list_network/list_network_model');
        $this->load->model('item_sim/item_sim_model');
    }

    public function index()
    {
        session_start();
    	//mang?
    	if(isset($_GET["snetwork"])) {
    		$networkId = $_GET["snetwork"];
    	}
    	//nho'm nao?
    	if(isset($_GET["sgroup"])) {
    		$groupId = $_GET["sgroup"];
    	}
    	//gia 0.5-1 0.5: 500k -> 1tr
    	if(isset($_GET["sgia"])) {
    		$gia = $_GET["sgia"];
    	}
    	//10 so hay 11 so
    	if(isset($_GET["sdauso"])) {
    		$dauso = $_GET["sdauso"];
    	}
    	if(isset($_GET['Textsim'])) {
    		$textsim = $_GET['Textsim'];
    	}
    	if(isset($_GET["sdau"])) {
    		$dau = $_GET["sdau"];
    	}
        
    	if(isset($_GET["ssapxep"])) {
    		$order = $_GET["ssapxep"];
    	}
        if(isset($_GET["page"])) {
            $page = $_GET["page"];
        }

    	//sapxep theo tang hay giam
    	//$orderPrice = $_GET["ssapxep"];
    	//so ko co trong sim
    	//$notNumber = $_GET['n'];
    	//so nut
    	//$countphone = $_GET['count'];
    	//tong diem
    	//$point = $_GET['point'];
    	//text search
    	if(!isset($networkId)) $networkId = 0;
    	if(!isset($groupId)) $groupId = 0;
    	if(!isset($gia)) $gia = 0;
    	if(!isset($dauso) || $dauso == 0) $dauso = '%';
    	if(!isset($dau) || $dau == 0) $dau = 0;
        
    	if(!isset($order) || $order == 0) $order = 1;
        if(!isset($page) || $page == 0) $page = 1;
    	if($order == 1) {
    		$order = 'ASC';
    	} else {
    		$order = 'DESC';
    	}
    	//echo $dauso;
    	
    	$giaArray = explode('-', $gia);
    	$startGia = 0;
    	$endGia = 5000;
    	if(count($giaArray) ==2) {
    		$startGia = $giaArray[0];
    		$endGia = $giaArray[1];
    	}
    	
		if(!isset($textsim) || $textsim == '') $textsim = '*';
        $textSim = $textsim;
        $_SESSION['search']['text_sim'] = $textSim;
        $_SESSION['search']['network_id'] = $networkId;
        $_SESSION['search']['group_id'] = $groupId;
        $_SESSION['search']['gia'] = $gia;
        $_SESSION['search']['order'] = $order;
        $_SESSION['search']['dau'] = $dau;
    	$textsim = str_replace('*', '%', $textsim);
        $data = array();

        //$page = (intval($page) != 0) ? intval($page) : 1;
        $langCode = $this->mycommon->fetchLangCode($this->_langCode);
        $this->load->language('landing/landing', $this->_langCode);
        $data['lang'] = $this->lang->line('landing_lang');
		$startGia = $startGia * 1000000;
		$endGia = $endGia * 1000000;
        //$this->layout->setMenuActive($id);
        
        //$data['headTitle'] = $tmpInfoDetail['name']. '-' . $tmpDetail['name'];
        //$data['catInfo'] = $tmpInfoDetail;
        $this->load->model('post/post_model');
        $this->load->library('Pagination');

        //$numPost = $this->item_sim_model->getCountListSimByNetworkIdAndGroupId($id, $detail_id, $order, $type);
        $numPost = $this->item_sim_model->searchCountArray($textsim, $networkId, $groupId, $startGia, $endGia, $dauso, $dau,  $order);
        $numPost = $numPost[0]['numrows'];
        //var_dump($numPost);

        $config['enable_query_strings'] = TRUE;
        $config['base_url'] = '/search/index?snetwork='.$networkId.'&sgroup='.$groupId.'&sdauso='.$dauso.'&sgia='.$gia.'&sdau='.$dau.'&Textsim='.$textsim.'&ssapxep=0';
        $config['prefix'] = '';
        $config['suffix']= '';
        $config['total_rows'] = $numPost;
        $config['per_page'] = 50;
        $config['cur_page'] = $page;
        $config['use_page_numbers'] = TRUE;
        $config['first_link'] = '«';
        $config['last_link'] = '»';
        $config['cur_tag_open'] = '<a class="active-page">';
        $config['cur_tag_close'] = '</a>';
        $config['num_links'] = 4;
       // $config['uri_segment'] = 4;
        $config['query_string_segment'] = 'page';
        $config['page_query_string'] = TRUE;
        $this->pagination->initialize($config);
        $start = ($page - 1) * $config['per_page'];
        $data['pagination'] = $this->pagination->create_links();
        $tmpPost = $this->item_sim_model->searchArray($textsim, $networkId,$groupId, $config['per_page'], $startGia, $endGia, $dauso, $dau, $order, $start);
        //$tmpPost = $this->item_sim_model->getListSimByNetworkIdAndGroupId($id, $detail_id, $config['per_page'], $start, $order, $type);

        $data['post'] = $tmpPost;
       
        $data['order'] = $order;
        $data['page'] = $page;
        $data['type'] = $dau;
        //var_dump($dau);die;
            
       // $tmpPost = $this->item_sim_model->searchArray($textsim, $networkId,$groupId, $startGia, $endGia, $dauso, $dau, $order);
        
        $data['post'] = $tmpPost;
        $data['network_id'] = $networkId;
        $data['group_id'] = $groupId;
        $data['gia'] = $gia;
        $data['text_sim'] = $textSim;
        
        
        $this->layout->view('search_sim/item_view', $data);
    }
    public function detail($id, $detail_id, $page = '')
    {	
    	$data = array();
    	$id = intval($id);
    	$detail_id = intval($detail_id);
    	//echo $id; die;
    	$page = (intval($page) != 0) ? intval($page) : 1;
    	$langCode = $this->mycommon->fetchLangCode($this->_langCode);
    	$this->load->language('landing/landing', $this->_langCode);
    	$data['lang'] = $this->lang->line('landing_lang');
    	
    	if ($id <= 0) {
    		show_404();
    	}
    	$tmpInfo = $this->list_network_model->getListNetwork($id, $langCode);
    	if($tmpInfo) {
    		$this->layout->setMenuActive($id);
    		$data['headTitle'] = $tmpInfo[0]['name'];
    		$data['catInfo'] = $tmpInfo[0];
    		$this->load->model('post/post_model');
    		$this->load->library('Pagination');
    		$numPost = $this->post_model->getNumPostByCatId($id);
    		$config['base_url'] = base_url('chuyen-muc/' . $this->mycommon->convertToAlias($tmpInfo[0]['name']) . '_' . $tmpInfo[0]['id']);
    		$config['prefix'] = ($this->_langCode == 'vietnam') ? 'trang_' : 'page_';
    		$config['suffix'] = '.html';
    		$config['total_rows'] = $numPost;
    		$config['per_page'] = 10;
    		$config['cur_page'] = $page;
    		$config['use_page_numbers'] = TRUE;
    		$config['first_link'] = '«';
    		$config['last_link'] = '»';
    		$config['cur_tag_open'] = '<a class="active-page">';
    		$config['cur_tag_close'] = '</a>';
    		$config['num_links'] = 4;
    		$config['uri_segment'] = 4;
    		$this->pagination->initialize($config);
    		$start = ($page - 1) * $config['per_page'];
    		$data['pagination'] = $this->pagination->create_links();
    		$tmpPost = $this->item_sim_model->getListSimByNetworkIdAndGroupId($id, $detail_id, $config['per_page'], $start, $langCode);
    
    		$data['post'] = $tmpPost;
    		$data['id'] = $id;
    	}
    	$this->layout->view('item_sim/item_network_view', $data);
    }
    
    public function birthday()
    {
        session_start();
        
        //10 so hay 11 so
        if(isset($_GET["Date_Day"])) {
            $dateDay = $_GET["Date_Day"];
        }
        if(isset($_GET['Date_Month'])) {
            $dateMonth = $_GET['Date_Month'];
        }
        if(isset($_GET["Date_Year"])) {
            $dateYear = $_GET["Date_Year"];
        }
        
        if(isset($_GET["type"])) {
            $type = $_GET["type"];
        }
        
        $tmpPost = $this->item_sim_model->searchBirthday($dateDay, $dateMonth,$dateYear, $type);
        
        $data = array();

        //$page = (intval($page) != 0) ? intval($page) : 1;
        $langCode = $this->mycommon->fetchLangCode($this->_langCode);
        $this->load->language('landing/landing', $this->_langCode);
        $data['lang'] = $this->lang->line('landing_lang');
            
       // $tmpPost = $this->item_sim_model->searchArray($textsim, $networkId,$groupId, $startGia, $endGia, $dauso, $dau, $order);
        
        $data['post'] = $tmpPost;
        $_SESSION['search']['date_day'] = $dateDay;
        $_SESSION['search']['date_month'] = $dateMonth;
        $_SESSION['search']['date_year'] = $dateYear;
        $_SESSION['search']['type'] = $type;
        
        $this->layout->view('search_sim/item_birthday_view', $data);
    }

}

?>
